/**
 * Created by FDG348 on 2017/10/19.
 */
$('#add_category_step1').change(function(){
    var add_category_step1 = $('#add_category_step1').val();
    var category_child = category_child_list[add_category_step1];
    var brand = brand_list[add_category_step1];
    $('#add_category_step2').empty();
    $('#add_category_step2').append('<option value="0">选择商品二级分类</option>');
    if(typeof category_child === 'object'){
        for(var i=0;i<category_child.length;i++){
            $('#add_category_step2').append('<option value="'+category_child[i]['id']+'">'+category_child[i]['category_name']+'</option>');
        }
    }

    $('#add_brand_id').empty();
    $('#add_brand_id').append('<option value="0">选择商品品牌</option>');
    if(typeof brand === 'object'){
    	//mod by lizenan 20180118 【后台管理】商品分类中选择品牌逻辑调整  过滤商品详情页品牌重复问题   468  start
    	//新增行 468
    	var brand_id_ed = [];
        for(var i=0;i<brand.length;i++){
        	//新增if判断 468
        	if($.inArray(brand[i]['brand_id'],brand_id_ed) != -1){
            	continue;
            }
            $('#add_brand_id').append('<option value="'+brand[i]['brand_id']+'">'+brand[i]['brand_name']+'</option>');
            //新增行 468
            brand_id_ed.push(brand[i]['brand_id']);
        }
        //mod by lizenan 20180118 【后台管理】商品分类中选择品牌逻辑调整  过滤商品详情页品牌重复问题   468  end
    }
})

var old_id = $('#navigation_identification').find('input[name=navigation_identification_id]:checked').val();
$('#old_id').val(old_id);
$('#navigation_identification').on('click','input[name=navigation_identification_id]:radio',function(){
	var old_id = $('#old_id').val();
	var new_id = $(this).val();
	if(old_id != undefined){
		$('input[name=navigation_identification_id]:checked').prop('checked',false);
		if(old_id != new_id){
			$(this).prop('checked',true);
			$('.second_level').addClass('hide');
			$(this).parent().next().removeClass('hide');
		}else{
			$(this).prop('checked',false);
			$('.second_level').addClass('hide');
		}
	}else{
		$(this).prop('checked',false);
		$('.second_level').addClass('hide');
	}
	$('#old_id').val(new_id);
});

$('#add_category_step2').change(function(){
    var add_category_step2 = $('#add_category_step2').val();
    var tag = tag_list[add_category_step2];
    $('#add_tag_id').empty();
    $('#add_tag_id').append('<option value="0">选择商品品类</option>');
    if(typeof tag === 'object'){
        for(var i=0;i<tag.length;i++){
            $('#add_tag_id').append('<option value="'+tag[i]['tag_id']+'">'+tag[i]['tag_name']+'</option>');
        }
    }
})

//提交新增商品表单
$('.ajax_post_add_goods').click(function(){
    var add_goods_name_cn = $('#add_goods_name_cn').val();
    var add_category_step1 = $('#add_category_step1').val();
    var add_category_step2 = $('#add_category_step2').val();
    var add_tag_id = $('#add_tag_id').val();
    var add_brand_id = $('#add_brand_id').val();
    var txt_cn = $('#add_brief_cn').val();
    var txt_other = $('#add_brief_other').val();
    //add by lizenan 20180129 【商城需求】商品详情页增加查看同品牌商品的按钮   488  start
    var see_more_product = $('#see_more_product').prop('checked') == true ? 1 : 0;
    $('#add_brief_cn').text(txt_cn);
    $('#add_brief_other').text(txt_other);

    if(add_goods_name_cn == ''){
        $.jBox.error('请输入商品中文名称', '错误');
        return false;
    }
    if(add_category_step1 == 0 || is_null(add_category_step1) == null){
        $.jBox.error('请选择商品一级分类', '错误');
        return false;
    }
    if(add_category_step2 == 0 || is_null(add_category_step2) == null){
        $.jBox.error('请选择商品二级分类', '错误');
        return false;
    }
    if(add_tag_id == 0 || is_null(add_tag_id) == null){
        $.jBox.error('请选择商品品类', '错误');
        return false;
    }
    if(add_brand_id == 0 || is_null(add_brand_id) == null){
        $.jBox.error('请选择商品品牌', '错误');
        return false;
    }
    var navigation_identification_id = [];
    $('input[name=navigation_identification_id]:checked').each(function(){
    	navigation_identification_id.push($(this).val());
    });
    //$.post($('#add_main_goods_form').attr('action'),$('#add_main_goods_form').serialize(),success,'json');
    $.post($('#add_main_goods_form').attr('action'),$('#add_main_goods_form').serialize()+'&see_more_product='+see_more_product+'&navigation_identification_id='+navigation_identification_id,success,'json');
    //add by lizenan 20180129 【商城需求】商品详情页增加查看同品牌商品的按钮   488  end
    return false;
    function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () {
            	history.back();
            }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
});

//添加关键字
$('#add_input_keyword').click(function(){
    var tr_length = $('#input_table_keyword').find('tr').length;

    var tr_length_ot = 0;

    $('#input_table').find('tr').each(function(){
        var data_length = $(this).attr('data-length');
        if(data_length != '' && data_length != undefined){
            tr_length_ot = eval(data_length);
        }
    })

    if(eval(tr_length_ot) > 0){
        tr_length = tr_length_ot + 1;
    }
    var class_len = $('#class_len').val();
    var div_class = 'dropdown-sin-'+class_len;
    var html = '<tr><td>';
    	//mod by lizenan 20180119 【后台需求】关键词维护改进   430  start
//    	html += '<div class="'+div_class+'">';
//   	 	html += '<select style="display:none;width:50%" name="keyword[]" placeholder="请选择">';
//   	 	if(keyword_data.length > 0){
//       	 	keyword_data.forEach(function(data,index){
//        		html += '<option value="'+data.name+'">'+data.name+'</option>';
//            });
//       	}
//    	html += '</select>';
//    	html += '</div>';
    	html += '<input value="" class="form-control" name="keyword[]" type="text">';
    	html += '</td>';
    	html += '<td><a class="del_input_table_tr" href="javascript:;">删除</a></td> </tr>';

    $('#input_table_keyword').append(html);

//    $('.'+div_class).dropdown({
//      readOnly: true,
//      input: '<input type="text" maxLength="20" placeholder="请输入搜索">'
//    });
//    class_len++;
//    $('#class_len').val(class_len);
    //mod by lizenan 20180119 【后台需求】关键词维护改进   430  end
});

//添加关键字
$('#add_input_sku').click(function(){
    var tr_length = $('#input_table_sku').find('tr').length;

    var tr_length_ot = 0;

    $('#input_table').find('tr').each(function(){
        var data_length = $(this).attr('data-length');
        if(data_length != '' && data_length != undefined){
            tr_length_ot = eval(data_length);
        }
    })

    if(eval(tr_length_ot) > 0){
        tr_length = tr_length_ot + 1;
    }
    var class_len = $('#class_len_2').val();
    var div_class = 'dropdown-sin-'+class_len;
    var html = '<tr><td><input name="sku[]" class="form-control"></td>';
    	html += '<td><a href="javascript:;" class="up_input_table_tr">上移</a> <a href="javascript:;" class="down_input_table_tr">下移</a> <a class="del_input_table_tr" href="javascript:;">删除</a></td></tr>';

    $('#input_table_sku').append(html);
    class_len++;
    $('#class_len_2').val(class_len);
});

$('table').delegate('.del_input_table_tr','click',function(){
    $(this).parent().parent().remove();
})

$('table').delegate('.up_input_table_tr','click',function(){
    var tr_index = $(this).parent().parent().prev().index();
    if(eval(tr_index) == 0){
        return true;
    }else{
        var obj = $(this).parent().parent().clone();
        $(this).parent().parent().prev().before(obj);
        $(this).parent().parent().remove();
    }

    $('#input_table').find('tr').each(function(){
        var tr_index = $(this).index();
        var data_length = $(this).attr('data-length');
        if(data_length != '' && data_length!= undefined && data_length != 0){
            $(this).attr('data-length',tr_index);
            $(this).find('.input_table_sort').val(tr_index);
        }
    })
})

$('table').delegate('.down_input_table_tr','click',function(){
    var tr_index = $(this).parent().parent().next().index();
    if(eval(tr_index) > eval(-1)){
        var obj = $(this).parent().parent().clone();
        $(this).parent().parent().next().after(obj);
        $(this).parent().parent().remove();
    }

    $('#input_table').find('tr').each(function(){
        var tr_index = $(this).index();
        var data_length = $(this).attr('data-length');
        if(data_length != '' && data_length!= undefined && data_length != 0){
            $(this).attr('data-length',tr_index);
            $(this).find('.input_table_sort').val(tr_index);
        }
    })
})

$('.upload-img-btn-main-pic').click(function(){
    $('.upload_file_goods_main_pic').trigger('click');
})

$('.upload_file_goods_main_pic').change(function(){
    if(this.value == ''){

    }else{
        $('#upload_goods_main_pic').submit();
    }
});

$('#upload_goods_main_pic').submit(function(e){
    e.preventDefault();
    $(this).ajaxSubmit({
        beforeSend:function(){
            /*$('.upload-img-btn').attr('disabled',true);*/
        },
        success:function(data){
            var result = $.parseJSON(data);
            if(result.code == 500){
                $.jBox.error(result.val, '错误');
            }else{
                $('#add_goods_main_pic').val(result.val);
                $('.show-img-btn-main-pic').attr('data-img',result.val);
                $('.show-img-btn-main-pic').show();
                
                $('#upload_goods_main_pic').find('input[type=file]').closest('form').get(0).reset();
            }
        }
    });

});

$('.show-img-btn-main-pic').click(function(){
    var img_url = $(this).attr('data-img');
    showImg(img_url);
})

function showImg(objKey){
    if(objKey == '' || objKey == null || objKey == undefined){
        $.jBox.error('数据为空', '错误');
        return false;
    }
    var json = {
        "title": "分类图片", //相册标题
        "id": 0, //相册id
        "start": 0, //初始显示的图片序号，默认0
        "data": [   //相册包含的图片，数组格式
            {
                "alt": "",//图片名
                "pid": 0, //图片id
                "src": $('#imageBathUrl').val() + objKey, //原图地址
                "thumb": "" //缩略图地址
            }
        ]
    };
    layer.photos({
        photos: json,
        area:['700px'],
    	shift: 1,
    	closeBtn:1,
    	offset:'100px',
    	shadeClose:true
    });
}

$("#add_brief_cn , #add_brief_other").xheditor({
	upImgUrl: '/common/xhedit-upload',
	upImgExt: "jpg,jpeg,gif,bmp,png",
	onUpload:insertUpload
},true);

function insertUpload(arrMsg)
{
	var i,msg;
	for(i=0;i<arrMsg.length;i++)
	{
		msg=arrMsg[i];
		$("#xheImgUrl").html(msg);
	}
}
